import PySimpleGUI as sg
import PySimpleGUI as psg
import sys
import win32api
import win32con
import cv2
import numpy as np
import time

def invert_image_colors(image):

    inverted_image = 255 - image

    return inverted_image

def capture_screen():
    # 使用PIL库来截取屏幕
    from PIL import ImageGrab
    screen = ImageGrab.grab()
    
    # 将PIL图像转换为numpy数组
    screen_np = np.array(screen)
    
    # 将RGB转换为BGR，因为OpenCV使用的是BGR颜色空间
    screen_np = cv2.cvtColor(screen_np, cv2.COLOR_RGB2BGR)
    
    # 显示截取的屏幕图像
    #cv2.imshow('Screen Capture', screen_np)
    
    # 等待按键后关闭窗口
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()
    
    # 返回截取的屏幕图像
    return screen_np
def select_roi(image):
    # 创建一个与原图相同大小的掩码，用于显示ROI选择窗口
    roi = image.copy()
    roi = cv2.selectROI("Select ROI", roi, showCrosshair=True, fromCenter=False)
    # 获取用户选择的ROI的坐标和大小
    x, y, w, h = roi
    #screen_np = cv2.cvtColor(roi, cv2.COLOR_RGB2BGR)
    selected_region = image[y:y+h, x:x+w]
    cv2.destroyAllWindows()
    #cv2.imshow('roi',selected_region)
    print('x,y,w,h',x,y,w,h)
    return selected_region,x,y,w,h
def move(image,x,y,w,h,i):
    roi = image.copy()
    selected_region = image[y:y+h, x:x+w]
    invert_image_colors0=invert_image_colors(selected_region)
    selected_region0 = image[y:y+h, x+i:x+w+i]
    combined = cv2.add(invert_image_colors0, selected_region0)
    return combined

# 定义界面A的布局
layout_A = [
    #[sg.Text("界面A")],
    [sg.Button("截屏", key="START")]
]
screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
# 创建界面A的窗口
window_A = sg.Window("界面A", layout_A, finalize=True, keep_on_top=True, grab_anywhere=True)
window_A_size = window_A.size
window_A.move(screen_width - window_A_size[0] - 10, screen_height - window_A_size[1] - 60)
# 事件循环
while True:
    event_A, values_A = window_A.read()
    if event_A == sg.WIN_CLOSED or event_A == "退出":
        break
    elif event_A == "START":
        # 隐藏界面A
        window_A.hide()
        # 定义界面B的布局
        screen_np=capture_screen()
        select_roi0,x,y,w,h=select_roi(screen_np)
        move0=move(screen_np,x,y,w,h,w)
      #select_roi0=select_roi(screen_np,i)
        roi_bytes = cv2.imencode('.png', move0)[1].tobytes()
        layout_B = [
            #[sg.Text("界面B")],
            [psg.Slider(range=(0, 800), default_value=w,
               expand_x=True, enable_events=True,
                orientation='horizontal', key='-SL-')],
            [sg.Button("重新截图", key="BACK")],
            [psg.Image(data=roi_bytes, key='-IMAGE-', enable_events=True)]
        ]
        # 创建并显示界面B
        window_B = sg.Window("界面B", layout_B)
        while True:
            event_B, values_B = window_B.read()
            if event_B == sg.WIN_CLOSED or event_B == "BACK":
                window_B.close()
                break
            if event_B == '-SL-':
                  transparency = values_B['-SL-'] / 100
                  #window['-TEXT-'].update(font=('Arial Bold', int(values['-SL-'])))
                  i=int(values_B['-SL-'])
                  move0=move(screen_np,x,y,w,h,i)
                  #select_roi0=select_roi(screen_np,i)
                  roi_bytes = cv2.imencode('.png', move0)[1].tobytes()
                  window_B['-IMAGE-'].update(data=roi_bytes)
        # 显示界面A
        window_A.un_hide()

# 关闭界面A
window_A.close()
